package agency.web.app.service.impl;

import java.util.ArrayList;
import java.util.List;





import org.hibernate.criterion.Criterion;
import org.hibernate.criterion.Restrictions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import agency.web.app.domain.Agent;
import agency.web.app.domain.Client;
import agency.web.app.domain.User;
import agency.web.app.repository.UserDao;
import agency.web.app.service.AgentService;

@Service
@Transactional
public class AgentServiceImpl extends UserServiceImpl implements AgentService{
	
	@Autowired
	private UserDao agentRepository;
	
	
	public void addAgent(Agent a) {
		agentRepository.add(a);
		
	}

	public void modifyAgent(Agent a) {
		agentRepository.modify(a);
		
	}

	public List<Agent> findAllAgents() {
		List<User> users = agentRepository.findAll();
		List<Agent> list = new ArrayList<Agent>();
		for (User u:users){
			if (isAgent(u))
				list.add((Agent) u);
		}
		return list;
	}

	public Agent findAgentById(int id) {
		return (Agent) agentRepository.findById(id);
	}

	public List<Agent> findAgentByName(String name) {
		
		Criterion criterion = Restrictions.eq("NAME",name );
		List<User> users = agentRepository.findByCriteria(criterion);
		List<Agent> list = new ArrayList<Agent>();	
		for (User u:users){
		
			if (isAgent(u))
				list.add((Agent) u);
		}
		return  list;
	}

	public List<Agent> findAgentByClient(Client client) {
		Criterion criterion = Restrictions.eq("CLIENTS",client.getUserId() );
		List<User> users = agentRepository.findByCriteria(criterion);
		List<Agent> list = new ArrayList<Agent>();	
		for (User u:users){
			if (isAgent(u))
				list.add((Agent) u);
		}
		return  list;
	}

	public void deleteAgentById(int id) {
		agentRepository.delete(id);
	}

	public void deleteAgent(Agent a) {
		agentRepository.delete(a);
		
	}

	public Agent agentRegistration(String login, String password) {
		Agent agent = (Agent) agentRepository.userRegistration(login, password);
		return agent;
	}

	public void logoutAgent(int agentId) {
		// TODO Auto-generated method stub
		
	}

	

	

	
	
		
	

}
